//index.js
//获取应用实例
const homeService = require('../../service/home.js');
const app = getApp()
const config = require("../../config/index.js");
const articleService = require('../../service/article.js');

Page({
  data: {
    title: '地铁社区',
    userInfo: {},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    contentInfo:{
      paper:[]
    },
    swiperConfig:{
      indicatorDots:true,
      autoplay:true,
      interval:5000,
      duration:1000,
      animationFun:"easing-function"
    },
    currentCity:"深圳",
    queryParams: {
      page: 1,
      num: 10,
      site: "",
      contentType: "",
      type: 1,
    },
    isLast: false,
    isShowBackTop:false
  },
  getContent(){
    let d = {
      ...this.data.queryParams
    };
    homeService.getIndex(d).then(res => {
      if(res.resCode == 0){
        this.setData({
          contentInfo:res.object
        })
      }
    });
  },
  onLoad: function () {
    this.getContent();
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onShow:function(){
    let city = wx.getStorageSync("DT-CITY");
    if (city != "" &&  this.data.currentCity != city){
      this.setData({
        currentCity: city
      });
    }
    this.getContent();
  },
  onPageScroll:function(e){
    let deviceInfo = app.globalData.deviceInfo;
    if (e.scrollTop > deviceInfo.screenHeight +50){
      this.setData({
        isShowBackTop:true
      })
    }else{
      this.setData({
        isShowBackTop:false
      })
    }
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getContentList() {
    let d = {
      ...this.data.queryParams
    }
    articleService.getArticleList(d).then(res => {
      if (res.resCode == 0) {
        if (!res.object.length) {
          this.setData({
            isLast: true
          });
          return false;
        }
        this.setData({
          'contentInfo.paper': [
            ...this.data.contentInfo.paper,
            ...res.object
          ]
        });
      }
    })
  },
  onReachBottom() {
    if (this.data.isLast) return false;
    this.setData({
      "queryParams.page": ++this.data.queryParams.page
    })
    this.getContentList();
  },
  onShareAppMessage(options) {
    return {
      imageUrl: "https://pic.ditieshequ.com/pic/daren-background-image.jpg",
      title: "地铁社区达人招募开始啦！享受社区粉丝经济-内容变现福利！更多福利等你来战！"
    }
  }
})
